import axios from 'axios'

axios.defaults.withCredentials = true

const dev = 'https://www.donglongdong.xyz/api/'
const prod = 'https://www.donglongdong.xyz/api/'
// create an axios instance
const service = axios.create({
  baseURL: dev,
  timeout: 1000 * 100
})

// request interceptor
service.interceptors.request.use(
  (config) => {
    // console.log(config);
    // if(config.params?.method == 'download'){
    //   config.headers['responseType'] = 'blob'
    // }
    if (localStorage.getItem('uooki-token')) {
      config.headers['AUTHORIZATION'] = localStorage.getItem('uooki-token')
    }
    return config
  },
  (error) => {
    // do something with request error
    console.log(error) // for debug
    return Promise.reject(error)
  }
)

// 响应拦截
service.interceptors.response.use(
  (response) => {
    const { code } = response.data
    switch (code) {
      case '401': // 登录过期
        break
      case '500': // 用户不存在
        break
      case '300': // 无token
        break
      default:
        return response
    }
  },
  (err) => {
    console.log('来到了response拦截failure中', err)
    return err
  }
)

export default service
